package com.example.howard.xiao7music.ui.fragment.RemoteFrament;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

import com.example.howard.xiao7music.MyApplication;
import com.example.howard.xiao7music.R;
import com.example.howard.xiao7music.presenter.RemoteSongPresenter;
import com.example.howard.xiao7music.service.DownloadService;
import com.example.howard.xiao7music.ui.Activity.RemoteActivity.SearchSongActivity;
import com.example.howard.xiao7music.ui.BottomPlayer;
import com.example.howard.xiao7music.ui.adapter.RemoteAdapter.SearchBySingerAdapter;
import com.example.howard.xiao7music.ui.adapter.RemoteAdapter.SearchBySongAdatpter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Howard on 2016/10/29.
 */
public class SearchBySongFragment extends Fragment {

    private EditText query;
    private ImageButton searchbtn;
    private ListView listView;
    private MyApplication myApplication;
    private SearchBySongAdatpter myAdapter;
    private List<Map<String, Object>> songlist = new ArrayList<>();
    private BottomPlayer bottomPlayer;
    private AdapterView.OnItemClickListener listener;
    private RemoteSongPresenter remoteSongPresenter;
    private SearchSongActivity searchSongActivity;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.search_by_song, null);
        init(view);
        myApplication= (MyApplication) getActivity().getApplication();
        searchSongActivity= (SearchSongActivity) this.getActivity();
        bottomPlayer=searchSongActivity.getBottomplayer();
        remoteSongPresenter=new RemoteSongPresenter(new RemoteSongPresenter.callback() {
            @Override
            public void getRankingSongList(List<Map<String, Object>> songlist) {

            }

            @Override
            public void getSearchBySinger(List<Map<String, Object>> list) {

//                songlist.addAll(list);
//                if (songlist!=null&&songlist.size()>0){
//                    myApplication.setSongdatalist(songlist);
//                    refreshListView();
//                }

            }

            @Override
            public void getSearchBySong(List<Map<String, Object>> list) {

                songlist=list;
                if (songlist!=null&&songlist.size()>0){
                    myApplication.setSongdatalist(songlist);
                    refreshListView();
                }
            }
        });

        searchbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                songlist.clear();
                String querytext=query.getText().toString();
                remoteSongPresenter.getSearchBySong(querytext);

            }
        });

        return view;
    }

    private void init(View view){
        query= (EditText) view.findViewById(R.id.search_by_song_searchText);
        searchbtn= (ImageButton) view.findViewById(R.id.search_by_song_searchbtn);
        listView= (ListView) view.findViewById(R.id.search_by_song_listview);
    }

    private  void refreshListView(){
        myAdapter  =new SearchBySongAdatpter(getContext(), songlist, new SearchBySongAdatpter.SearchBySongCallback() {
            @Override
            public void download(int position) {
                dialog(position);
            }
        });
        listView.setAdapter(myAdapter);
        listener = resetClickListener();
        listView.setOnItemClickListener(listener);
    }

    private AdapterView.OnItemClickListener resetClickListener() {
        AdapterView.OnItemClickListener songListclickListener = new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                //异步延迟，在回调中set
//                myApplication.setSongdatalist(songlist);
                bottomPlayer.callByList(arg2);
            }
        };
        return songListclickListener;
    }

    protected void dialog(final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setMessage("确认要下载歌曲到本地吗？");

        builder.setTitle("请求下载歌曲");

        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent=new Intent(getActivity(), DownloadService.class);
//                Log.e("Activity",rankingsongbeanlist.get(position).getSong_id());
                intent.putExtra("songID",songlist.get(position).get("ID").toString());
                getActivity().startService(intent);
                dialog.dismiss();
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        builder.create().show();
    }
}
